package vn.demand.dragon.controller
{
	import com.salesbuilder.control.TabManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	import vn.demand.dragon.model.CompanyProxy;
	import vn.demand.dragon.view.CustomerListMediator;
	import vn.demand.dragon.view.CustomerSearchMediator;
	import vn.demand.dragon.view.components.CustomerList;
	
	public class OpenCustomerListCommand extends SimpleCommand
	{
		override public function execute( note:INotification ) :void    
		{
			if (facade.hasMediator(CustomerListMediator.NAME)) {
				TabManager.openTab(CustomerList, CustomerListMediator.NAME);
				return;
			}
			var ui:CustomerList = TabManager.openTab(CustomerList, CustomerListMediator.NAME) as CustomerList;
            facade.registerProxy(new CompanyProxy(CompanyProxy.NAME));
           
            var customerProxy:CompanyProxy  = facade.retrieveProxy(CompanyProxy.NAME) as CompanyProxy;
            RIALogger.debug(this, "Chay trong ham execute cua OpenCustomerList");
            if (customerProxy == null) {
            	customerProxy = new CompanyProxy(CompanyProxy.NAME);
            	facade.registerProxy(customerProxy);
            }
            customerProxy.loadCustomers();
            
            facade.registerMediator(new CustomerListMediator(ui));
            facade.registerMediator(new CustomerSearchMediator(CustomerSearchMediator.NAME, ui.searchForm));
        }
	}
}